package com.ag.main.v1;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
 * @Description： 读取并打印图片的像素信息
 * */
public class ImageReader {
    public static void main(String[] args) {
        try {
            // 读取图片文件
//            File file = new File("./static/33.png");
            File file = new File("./output/33.png");
            BufferedImage image = ImageIO.read(file);
            // 获取图片的宽度和高度
            int width = image.getWidth();
            int height = image.getHeight();
            // 遍历每个像素，获取其颜色值
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    int pixel = image.getRGB(x, y);
                    int alpha = (pixel >> 24) & 0xff;
                    int red = (pixel >> 16) & 0xff;
                    int green = (pixel >> 8) & 0xff;
                    int blue = pixel & 0xff;
                    // 处理颜色值，这里只是简单地输出
                    System.out.println("Pixel at (" + x + "," + y + "): " +
                            "alpha=" + alpha + ", red=" + red + ", green=" + green + ", blue=" + blue);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
